Python Basics - Lesson 05 - Working with Lists
Python Basics · Lesson 05
Working with Lists
A list stores several values in one variable. Lists are useful for keeping related information together—like errands, scores, or favorite foods—and processing each item with a loop.
append(), find the list length, and loop through its values.
1. Create a list
Put items inside square brackets and separate them with commas. A list can hold strings, numbers, or other values.
favorite_foods = ["mango", "noodles", "tacos"]
scores = [8, 10, 7]
2. Read an item by its index
Python starts counting list indexes at 0. That means the first item is at index 0, the second at index 1, and so on.
favorite_foods = ["mango", "noodles", "tacos"]
print(favorite_foods[0])
print(favorite_foods[1])
These lines display mango and noodles. An index
that is outside the list’s range causes an IndexError.
You can replace an item by assigning a new value to its index:
favorite_foods[1] = "dumplings"
print(favorite_foods)
3. Add items and check the length
The append() method adds one item to the end. The
len() function tells you how many items are in the list.
favorite_foods.append("berries")
print(len(favorite_foods))
The updated list has four items, so len(favorite_foods)
displays 4.
4. Use a loop to process every item
A for loop can visit each item without needing to count indexes
yourself.
errands = ["pick up a package", "buy apples", "call the dentist"]
for errand in errands:
print(f"- {errand}")
On each pass, errand refers to the next item in the list.
Hands-on exercises
- Create a list with three hobbies. Print the first hobby, then change the second hobby and print the updated list.
-
Start with an empty list of tasks. Use
append()to add two tasks, then use aforloop to print each one.
Show Solution & Common Mistakes
Exercise 1 — one possible solution
hobbies = ["gardening", "sketching", "cycling"]
print(hobbies[0])
hobbies[1] = "painting"
print(hobbies)
Exercise 2 — one possible solution
tasks = []
tasks.append("water the plants")
tasks.append("reply to a message")
for task in tasks:
print(task)
Common mistakes:
- Trying to access the first item with index 1. The first index is 0.
- Using an index that does not exist. A three-item list has indexes 0, 1, and 2.
-
Writing
tasks = tasks.append("new task"). Theappend()method changes the list directly; call it without assigning its result. -
Putting the
forloop’s print statement outside the indented loop body.
Quick quiz
Choose an answer, then select Check answer.
1. What index refers to the first item in a list?
2. Which method adds an item to the end of a list?
3. What does len(["red", "green", "blue"]) return?
Mini-project: Three-Item Errand List
Make a small program that collects three errands, stores them in a list, and prints the finished list.
Requirements:
- Start with an empty list.
- Ask for three errands using a loop.
- Add each answer to the list with
append(). - Print the number of errands and then each errand.
Hints: Use range(1, 4) to repeat three times.
Use len() to count the saved items.
Example run:
Errand 1: buy apples
Errand 2: pick up a package
Errand 3: water the plants
You have 3 errands:
- buy apples
- pick up a package
- water the plants
Show Full Project Solution
errands = []
for number in range(1, 4):
errand = input(f"Errand {number}: ")
errands.append(errand)
print(f"You have {len(errands)} errands:")
for errand in errands:
print(f"- {errand}")
The first loop gathers the answers. The second loop displays each saved item. Both loops process the same list for different purposes.
Key takeaways
- A list keeps multiple values in one ordered collection.
- Indexes start at 0; use a valid index to read or replace an item.
append()adds an item to the end, andlen()counts items.- A
forloop can visit every item in a list.